package com.eastshop.order.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.eastshop.order.dao.OrderDao;
import com.eastshop.order.vo.Order;
import com.eastshop.order.vo.Orderitem;
import com.eastshop.utils.PageBean;

/**
 * 订单管理的Service层代码
 * @author admin
 *
 */
@Transactional
public class OrderService {
	// 注入OrderDao
	private OrderDao orderDao;

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	
	// Service层保存订单的方法
	public Integer save(Order order) {
		return orderDao.save(order);
	}
	
	// Service层分页查询我的订单的方法
	public PageBean<Order> findByUidWithPage(int page, Integer uid) {
		PageBean<Order> pageBean = new PageBean<Order>();
		// 设置当前页
		pageBean.setPage(page);
		// 设置每页显示的记录数
		int limit = 5;
		pageBean.setLimit(limit);
		// 设置总的记录数
		int totalCount = orderDao.findCountByUid(uid);
		pageBean.setTotalCount(totalCount);
		// 设置总的页数
		int totalPage = 0;
		if(totalCount % limit == 0){
			totalPage = totalCount / limit;
		}else{
			totalPage = totalCount / limit + 1;
		}
		pageBean.setTotalPage(totalPage);
		// 设置每页显示的数据集合
		int begin = (page - 1) * limit;
		List<Order> list = orderDao.findByUidWithPage(uid, begin, limit);
		pageBean.setList(list);
		return pageBean;
	}
	
	// Service层根据订单id查询订单
	public Order findByOid(Integer oid) {
		return orderDao.findByOid(oid);
	}

	// Service层更新订单的信息
	public void update(Order currOrder) {
		orderDao.update(currOrder);
	}

	// Service层分页查询所有订单的方法
	public PageBean<Order> findAllWithPage(int page) {
		PageBean<Order> pageBean = new PageBean<Order>();
		// 设置当前页
		pageBean.setPage(page);
		// 设置每页显示的记录数
		int limit = 5;
		pageBean.setLimit(limit);
		// 设置总的记录数
		int totalCount = orderDao.findCount();
		pageBean.setTotalCount(totalCount);
		// 设置总的页数
		int totalPage = 0;
		if(totalCount % limit == 0){
			totalPage = totalCount / limit;
		}else{
			totalPage = totalCount / limit + 1;
		}
		pageBean.setTotalPage(totalPage);
		// 设置每页显示的数据集合
		int begin = (page - 1) * limit;
		List<Order> list = orderDao.findWithPage(begin, limit);
		pageBean.setList(list);
		return pageBean;
	}

	// Service层根据订单id查询订单项
	public List<Orderitem> findOrderItemByOid(Integer oid) {
		return orderDao.findOrderItemByOid(oid);
	}
	
}
